package com.example.onlineeducation.ui.main


import android.os.Bundle
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseFragment
import com.example.onlineeducation.data.constant.ExamConstants
import com.example.onlineeducation.data.constant.StudyConstants
import com.example.onlineeducation.data.constant.TopicType
import com.example.onlineeducation.data.entity.ExamEntity
import com.example.onlineeducation.data.entity.TopicEntity
import com.example.onlineeducation.databinding.TopicChildFragmentBinding
import com.example.onlineeducation.databinding.TopicChildItemBinding
import com.example.onlineeducation.ui.online.OnlineExaminationAc
import com.example.onlineeducation.ui.topic.StudyContentActivity
import com.example.onlineeducation.utils.ThisAppItemDecoration
import com.example.onlineeducation.utils.YcTimeUtils
import com.example.onlineeducation.viewmodel.MainHomeVM
import com.example.onlineeducation.viewmodel.MainTopicVM
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.extension.ycToNoEmpty
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart
import com.yc.jetpacklib.widget.dialog.YcCommonDialog

/*
* 专题
* */
class SearchResultFragment(val type: String) :
    BaseFragment<TopicChildFragmentBinding>(TopicChildFragmentBinding::inflate) {
    private val mViewModel: MainTopicVM by ycViewModels()
    private val mMainHomeViewModel: MainHomeVM by ycViewModelsActivity()
    private var mProId: String? = App.mInstant.mViewModel.mProInfoEntity.value?.proId
    private var mKeyName: String = ""
    private lateinit var mRefreshUtil: YcRefreshSpecialUtil

    private val mDialog: YcCommonDialog by lazy {
        YcCommonDialog(this.requireContext(), this)
    }
    private val mTopicAdapter by YcRecyclerViewAdapter.ycLazyInitApply<TopicEntity, TopicChildItemBinding>(
        TopicChildItemBinding::inflate
    ) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.examImage, errorImg =  R.drawable.ic_topic)
            tvStudyName.text = it.topicName
            tvTotalGrade.visibility = View.INVISIBLE
            tvStudyTime.text = "已学习" + YcTimeUtils.intSecondToMinStr(it.topicStudyTime.toInt())
            tvState.text = StudyConstants.showStudyResultText(it.topicPass)
            tvState.ycSetTextColorRes(StudyConstants.showStudyResultTextColor(it.topicPass))
            ivState.setImageResource(StudyConstants.showStudyResultIcon(it.topicPass))
        }
        mItemClick = {
            StudyContentActivity.newInstance(
                requireActivity(),
                it.topicId,
                it.needStudyTime,
                it.topicStudyTime
            )
        }
    }

    private val mExamAdapter by YcRecyclerViewAdapter.ycLazyInitApply<ExamEntity, TopicChildItemBinding>(
        TopicChildItemBinding::inflate
    ) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.examImage, errorImg =  R.drawable.ic_topic)
            tvStudyName.text = it.examName
            tvTotalGrade.visibility = View.INVISIBLE
            tvStudyTime.text = it.examNeedPoint
            tvState.text = ExamConstants.showResultText(it.examPass)
            tvState.ycSetTextColorRes(ExamConstants.showResultTextColor(it.examPass))
            ivState.setImageResource(ExamConstants.showResultIcon(it.examPass))
        }
        mItemClick = { mExamEntity ->
            if (mExamEntity.isExceedTime) {
                mViewModel.getCheckStudyTime(mProId!!, mExamEntity.topicId,mExamEntity)
            } else {
                mDialog.setMsg("考试已结束").showSingle()
            }


        }
    }

    override fun TopicChildFragmentBinding.initView(view: View, savedInstanceState: Bundle?) {
        when (type) {
            TopicType.Type.TOPIC -> {
                rvList.adapter = mTopicAdapter
                mRefreshUtil = YcRefreshSpecialUtil(
                    requireActivity(),
                    srlList,
                    mTopicAdapter,
                    YcSpecialViewSmart(rvList, flRefresh),
                    false
                )
            }
            TopicType.Type.EXAM -> {
                rvList.adapter = mExamAdapter
                mRefreshUtil = YcRefreshSpecialUtil(
                    requireActivity(),
                    srlList,
                    mExamAdapter,
                    YcSpecialViewSmart(rvList, flRefresh),
                    false
                )
            }
        }
        rvList.addItemDecoration(ThisAppItemDecoration())
        srlList.setNoMoreData(true)

        mRefreshUtil.mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
            if (isRefresh) {
                getData()
            }
        }
        mViewModel.mTopicListForSearchDataModel.observe {
            it.doSuccess { data ->
                mTopicAdapter.addAllData(data)
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }

        mViewModel.mExamListForSearchDataModel.observe {
            it.doSuccess { data ->
                mExamAdapter.addAllData(data)
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }
        mMainHomeViewModel.mSearchEntity.observe {
            mKeyName = it.ycToNoEmpty("")
            mRefreshUtil.startRefresh()
        }
        mViewModel.mCheckStudyTimeDataModel.observe {//TODO 多次注册有bug
            it.doSuccess {
                OnlineExaminationAc.newInstance(
                    requireContext(), it.id, it.examName, it.time, it.timeEnd
                )
            }.ycShowNetError()
        }
    }

    private fun getData() {
        when (type) {
            TopicType.Type.TOPIC -> mViewModel.getTopicForSearchList(
                mProId!!,
                mKeyName
            )
            TopicType.Type.EXAM -> mViewModel.getExamForSearchList(
                mProId!!,
                mKeyName
            )
        }
    }

}
